/**
 * 生成简单crud
 */

const fs = require('fs');
const path = require('path');
const readline = require("readline");


function titleCase(str) {
    newStr = str.slice(0, 1).toUpperCase() + str.slice(1).toLowerCase();
    return newStr;
}

function run() {
    let entityClassName = "";
    // 创建readline接口实例
    let r1 = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    })
    //close事件监听
    r1.on("close", function () {
        // 结束程序
        process.exit(0);
    })
    //调用接口方法
    r1.question("请输入类名\t", function (answer) {
        var re = new RegExp("^[a-zA-Z]+$");
        if (re.test(answer)) {
            entityClassName = titleCase(answer);
            generate(entityClassName);
        } else {
            console.log("输入类目错误，请重新输入");
        }
        r1.close();
    })
}

async function generate(entityClassName) {

    let lowEntityClassName = entityClassName.toLowerCase();

    // 1.在src文件夹下创建entityClassName的文件夹
    fs.mkdirSync(path.join(__dirname, `../${lowEntityClassName}`), { recursive: true });
    fs.mkdirSync(path.join(__dirname, `../${lowEntityClassName}/dto`), { recursive: true });
    fs.mkdirSync(path.join(__dirname, `../${lowEntityClassName}/entities`), { recursive: true });
    // 2.读取并写入create.dto
    let filePath = path.join(__dirname, './template/dto/create-template.dto.template');
    let res = fs.readFileSync(filePath).toString()
    res = res.replaceAll("${template}", entityClassName)
    fs.writeFileSync(path.join(__dirname, `../${entityClassName}/dto/create-${lowEntityClassName}.dto.ts`), res)
    // 3.读取并写入update.dto
    filePath = path.join(__dirname, './template/dto/update-template.dto.template');
    res = fs.readFileSync(filePath).toString()
    res = res.replaceAll("${template}", entityClassName)
    res = res.replaceAll("template", lowEntityClassName)
    fs.writeFileSync(path.join(__dirname, `../${entityClassName}/dto/update-${lowEntityClassName}.dto.ts`), res)
    // 4.读取并写入entities
    filePath = path.join(__dirname, './template/entities/template.entity.template');
    res = fs.readFileSync(filePath).toString()
    res = res.replaceAll("${template}", entityClassName)
    res = res.replaceAll("template", lowEntityClassName)
    fs.writeFileSync(path.join(__dirname, `../${entityClassName}/entities/${lowEntityClassName}.entity.ts`), res)
    // 5.读取并写入service
    filePath = path.join(__dirname, './template/template.service.template');
    res = fs.readFileSync(filePath).toString()
    res = res.replaceAll("${template}", entityClassName)
    res = res.replaceAll("template", lowEntityClassName)
    fs.writeFileSync(path.join(__dirname, `../${entityClassName}/${lowEntityClassName}.service.ts`), res)
    // 6.读取并写入controller
    filePath = path.join(__dirname, './template/template.controller.template');
    res = fs.readFileSync(filePath).toString()
    res = res.replaceAll("${template}", entityClassName)
    res = res.replaceAll("template", lowEntityClassName)
    fs.writeFileSync(path.join(__dirname, `../${entityClassName}/${lowEntityClassName}.controller.ts`), res)
    // 7.读取并写入module
    filePath = path.join(__dirname, './template/template.module.template');
    res = fs.readFileSync(filePath).toString()
    res = res.replaceAll("${template}", entityClassName)
    res = res.replaceAll("template", lowEntityClassName)
    fs.writeFileSync(path.join(__dirname, `../${entityClassName}/${lowEntityClassName}.module.ts`), res)


}

run()

module.export = { titleCase }